package com.ghca.myinfo3.system.controller;

import com.ghca.myinfo3.system.entity.User;
import com.ghca.myinfo3.system.service.IPermissionService;
import com.ghca.myinfo3.system.service.IUserService;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * Created by gh on 2016/12/19.
 */

@Controller
public class IndexController {

    @Resource
    private Environment environment;

    @Resource
    private IUserService userService;
    @Resource
    private IPermissionService permissionService;

    @RequestMapping("/login")
    public String login() {
        return "login";
    }


    @RequestMapping("/index")
    public String index(String username, String password, Model model) {
        for (String profile : environment.getActiveProfiles()) {
            System.out.println("PROFILE:" + profile);
            if ("dev".equals(profile)) {
                username = "test";
                password = "test";
            }
        }
        User user =  userService.login(username, password);
        if(user == null) {
            return "login";
        }
        else {
            model.addAttribute("user", user);
            model.addAttribute("permissions", permissionService.findByUsername(username));
            return "index";
        }
    }
}
